<%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2020/11/23
  Time: 11:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<button type="button" class="layui-btn" id="c">添加</button>
<form class="layui-form"  style="display: none" id="d" >
    <div class="layui-form-item">
        <label class="layui-form-label">员工姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="emsName" required lay-verify="required" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">员工薪资</label>
        <div class="layui-input-inline">
            <input type="text" name="salary" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <%--下拉列表框--%>
            <select name="deptId">
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <input type="text" id="upload" name="picPath">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="go">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test"></table>
<script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.1.js"></script>
<script>
    layui.use(['table',"layer","form","upload"], function(){
        var table = layui.table;
        var layer=layui.layer;
        var form=layui.form;
        var upload=layui.upload;
        //第一个实例
        table.render({
            elem: '#demo'
            ,url: 'employees' //数据接口
            ,page: false
            ,cols: [[ //表头
                {field: 'emsId', title: 'ID' }
                ,{field: 'emsName', title: '员工姓名'}
                ,{field: 'salary', title: '员工薪资'}
                ,{title: '部门', templet:function (d) {
                    return d.depart.deptName
                    }}
                ,{ title: '操作', toolbar:'#barDemo'}
            ]]
        });
        //select下拉框的值
        $.ajax({
            url:"departs",
            dataType:"json",
            success:function (data) {
                var html="";
                //遍历集合
                for (var a=0;a<data.length;a++) {
                    html += "<option value='" + data[a].id + "'>" + data[a].deptName + "</option>"
                }
                $("[name=deptId]").html(html)
                //重新渲染select下拉框
                form.render();
            }
        })

        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: 'employees/upload' //上传接口
            ,done: function(res){
                $("#upload").val(res.fileName);
            }
            ,error: function(){
                //请求异常回调
            }
        });
        form.on("submit(go)", function(obj){
            var filed= obj.field;
            console.log(filed)
            $.ajax({
                url:"employees",
                contentType:"application/json",
                data:JSON.stringify(filed),
                type:"post",
                success:function (data) {
                    console.log(data)
                    //关闭弹出层
                    layer.closeAll();
                    //清空表单中的数据
                    $("#d").get(0).reset();
                    table.reload("demo");
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
<script type="text/javascript">
    $("#c").click(function () {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                area: ['600px', '300px'],
                title:"添加",
                type: 1,
                content: $('#d') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        })
    })
</script>
<script type="text/html" id="barDemo">
    <%--  <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>--%>
    <a class="layui-btn layui-btn-xs" lay-event="edit" id="updates">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
